package org.pierre.inputbenchmark.activity;

import android.app.Activity;
import android.os.Bundle;

public abstract class FrequencyBenchmarkActivity extends Activity
{
	private static final int MAX_FREQUENCY_SAMPLE = 50;
	
	private double[] frequencies;
	private long previousTime;
	private int currentPosition;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		this.frequencies = new double[MAX_FREQUENCY_SAMPLE];
		this.previousTime = 0;
		this.currentPosition = 0;
	}
	
	protected int getAverageFrequency(long currentTime)
	{
		double frequency = 1000 / (double) (currentTime - this.previousTime);
		this.previousTime = currentTime;
		
		this.frequencies[this.currentPosition] = frequency;
		this.currentPosition++;
		if (this.currentPosition == MAX_FREQUENCY_SAMPLE)
		{
			this.currentPosition = 0;
		}
		
		double sum = 0;
		
		for (double d : this.frequencies)
		{
			sum += d;
		}
		
		int averageFrequency = (int) (sum / MAX_FREQUENCY_SAMPLE);
		
		return averageFrequency;
	}
}
